package com.yowasa.xiuxian.engine.message.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.bot.message.Message;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuxianUserBaseInfoPO;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.XiuxianUserBaseInfoMapper;
import com.yowasa.xiuxian.service.helper.LocalHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.DateXUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.Date;

/**
 * 处理新建用户
 */

@CMD(name = "/开始修仙", outCmd = true)
public class CreateUserHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private XiuxianUserBaseInfoMapper xiuxianUserBaseInfoMapper;
    @Autowired
    private UserHelper userHelper;
//{
//  "content": "",      // 消息内容
//  "from_user_id": 0,  // 发送者 id
//  "send_at": 0,       // 发送时间的时间戳
//  "room_id": 0,       // 房间 id
//  "object_name": 1,   // 目前只支持文本类型消息
//  "nickname": "",     // 用户昵称
//  "msg_uid": "",      // 消息 id
//  "bot_msg_id": "",   // 如果被回复的消息从属于机器人，则该字段不为空字符串
//  "villa_id": 0       // 大别野 id
//}

    @Override
    public void execute(SendMessage message) {
        String nickname = message.getNickname();
        String from_user_id = message.getFrom_user_id();
        //根据userId和死亡状态找存档
        boolean exists = xiuxianUserBaseInfoMapper.exists(new LambdaQueryWrapper<XiuxianUserBaseInfoPO>()
                .eq(XiuxianUserBaseInfoPO::getDead, 0)
                .eq(XiuxianUserBaseInfoPO::getUserId, from_user_id)
        );
        if (exists) {
            throw new HandlerException("已经创建了用户，无需再次创建！");
        }
        //之前死亡的用户
        XiuxianUserBaseInfoPO deadUser = xiuxianUserBaseInfoMapper.selectOne(new LambdaQueryWrapper<XiuxianUserBaseInfoPO>()
                .eq(XiuxianUserBaseInfoPO::getDead, 1)
                .eq(XiuxianUserBaseInfoPO::getUserId, from_user_id)
        );
        if (deadUser != null) {
            int days = DateXUtil.getFrom(deadUser.getDeadAt());
            Date end = DateXUtil.calEndTime(deadUser.getDeadAt(), 180);
            if (days < 180) {
                throw new HandlerException(MessageFormat.format("灵魂转世需要半年时间[{0}]", DateXUtil.formatTime(end)));
            }
            xiuxianUserBaseInfoMapper.delete(new LambdaQueryWrapper<XiuxianUserBaseInfoPO>()
                    .eq(XiuxianUserBaseInfoPO::getUserId, from_user_id));
        }
        UserBO bo = userHelper.initUser(nickname);
        bo.setGuildId(message.getVilla_id());
        bo.setUserId(from_user_id);
        userHelper.calHistoryUpper(bo);
        //给用户设置子频道权限，牛逼啊，这样用户只有在当前自己在的地图才能说话
        //高手，真高手
//        localHelper.addUser2Local(bo, MapEnum.QING_QUAN_ZHEN);
        bo.setLocal(MapEnum.QING_QUAN_ZHEN.getId());
        bo.setLocalName(MapEnum.QING_QUAN_ZHEN.getName());
        bo.buildBaseInfo();
        xiuxianUserBaseInfoMapper.insert(bo);
        messageHelper.replyMys(message, MessageFormat.format("被{0}三神眷顾的{1}来到了提瓦特\n /指令 可查看所有指令，/帮助 可以查看新手教程和每个指令的作用", ElementEnum.getLingGenDesc(bo.getElement(), XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY), bo.getName()));

    }


    public String desc(){
        return new StringBuffer().append("开始游玩的指令，使用此指令可以初始化自己的个人信息\n")
                .toString();

    }

}
